package org.dey.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.dey.entity.Result;
import org.dey.mapper.ArgetManagementMapper;
import org.dey.pojo.ArgetManagement;
import org.dey.pojo.PageVo;
import org.dey.service.ArgetManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class ArgetManagementServiceImpl extends ServiceImpl<ArgetManagementMapper, ArgetManagement> implements ArgetManagementService {
    @Autowired
    ArgetManagementMapper argetManagementMapper;

    @Override
    public Map addAm(ArgetManagement argetManagement) {


        Boolean num = this.save(argetManagement);

        Map map = new HashMap();
        map.put("code", "0");
        map.put("msg", "添加失败");
        if (num) {
            map.put("code", "1");
            map.put("msp", "添加成功");
        }
        return map;
    }

    @Override
    public int updateAm(ArgetManagement argetManagement) {
        int num = 0;
        boolean b = this.updateById(argetManagement);
        if (b) {
            num = 1;
        }
        return num;
    }

    @Override
    public Result deleteAm(int amid) {
//
//        QueryWrapper queryWrapper = new QueryWrapper();
//        queryWrapper.eq("id", amid);
//        this.removeById(amid);
        Result result = null;
        try {

            this.argetManagementMapper.deleteByAmid(amid);
            result = new Result(true, "删除报告成功");
        } catch (Exception e) {
            result = new Result(true, "删除报告失败");
        }
        return result;
    }

    @Override
    public ArgetManagement queryAmById(int amid) {
        return this.getById(amid);
    }

    @Override
    public PageVo<ArgetManagement> queryAm(ArgetManagement argetManagement, int pageNum, int pagesize) {
        QueryWrapper<ArgetManagement> queryWrapper = new QueryWrapper<ArgetManagement>();
        if (StringUtils.isNotBlank(argetManagement.getAmname())) {
            queryWrapper.like("amname", argetManagement.getAmname());
        }
        PageHelper.startPage(pageNum, pagesize);

        List<ArgetManagement> list = this.list(queryWrapper);

        PageInfo<ArgetManagement> pageInfo = new PageInfo<>(list);

        PageVo<ArgetManagement> pageVo = new PageVo<>();
        pageVo.setRows(pageInfo.getList());
        pageVo.setTotal(pageInfo.getTotal());

        return pageVo;
    }


}
